package com.menu.Controller;

import com.menu.Pojo.SecurityUser;
import com.menu.Service.MessageService;
import com.menu.Utils.CommonUtil;
import com.menu.Utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "用户反馈留言接口")
@RestController
public class MessageController {

    @Autowired
    private CommonUtil commonUtil;
    @Autowired
    private MessageService messageService;

    @ApiOperation("用户对订单或商品发出反馈或留言")
    @PostMapping("/User/sendMessage")
    public ResponseResult sendMessage(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser, String question, Integer type, Long extendId,
                                      MultipartFile ...multipartFile){
        if (!commonUtil.verifyStirng(question)){
            return ResponseResult.error("内容不能为空！");
        }
        if (type == null || !(type == 0 || type == 1)){
            return ResponseResult.error("类型信息有误！");
        }
        if (extendId == null){
            return ResponseResult.error("相关id有误！");
        }
        return messageService.sendMessage(securityUser.getUserId(),question,type,extendId,multipartFile);
    }

    @ApiOperation("用户查询自己的反馈")
    @GetMapping("/User/queryMessage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state",value = "0 未处理 1已处理"),
            @ApiImplicitParam(name = "type",value =  "0 订单反馈 1 留言")
    })
    public ResponseResult queryMessage(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,
                                       Integer pageStart,Integer pageSize
                                       ,Integer type,Integer state){
        if (pageStart == null || pageStart <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (pageSize == null || pageSize <= 0){
            return ResponseResult.error("参数有误！");
        }
        if (state == null || !(state == 1 || state == 0)){
            return ResponseResult.error("状态有误！");
        }
        if (type == null || !(type == 1 || type == 0)){
            return ResponseResult.error("状态有误！");
        }
        return messageService.queryMessage(securityUser.getUserId(),type,state,pageStart,pageSize);
    }


    @ApiOperation("管理员查询反馈或回复")
    @GetMapping("/Admin/queryMessage")
    public ResponseResult aQueryMessage(Integer type){
        Integer state = 0;
        if (type == null || !(type == 1 || type == 0)){
            return ResponseResult.error("状态有误！");
        }
        return messageService.AqueryMessage(type,state);
    }

    @ApiOperation("管理员通过MessageId查询详情")
    @GetMapping("/Admin/queryMessageDetails")
    public ResponseResult queryForDetail(Long messageId){
        if (messageId == null){
            return ResponseResult.error("参数有误！");
        }
        return messageService.queryForDetail(messageId);
    }

    @ApiOperation("管理员进行处理")
    @PostMapping("/Admin/resolve")
    public ResponseResult resolve(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,
                                  Long messageId,String content){
        if (messageId == null){
            return ResponseResult.error("参数有误！");
        }
        return messageService.resolve(securityUser.getUserId(),content,messageId);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/User/deleteMessage")
    public ResponseResult deleteMessage(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser, Integer ...messageId){
        if (messageId == null){
            return ResponseResult.error("参数有误！");
        }
        return messageService.deleteMessage(messageId,securityUser.getUserId());
    }

    @ApiOperation("是否有未读的留言反馈")
    @GetMapping("/User/queryForMessage")
    public ResponseResult queryForMessage(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        return messageService.queryForMessage(securityUser.getUserId());
    }

    @ApiOperation("用户通过MessageId查询详情")
    @GetMapping("/User/queryMessageDetails")
    public ResponseResult UqueryForDetail(@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser,
                                          Long messageId){
        if (messageId == null){
            return ResponseResult.error("参数有误！");
        }
        return messageService.queryForDetail(messageId,securityUser.getUserId());
    }
}
